﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicsEditor
{
    public partial class ResizeForm : Form
    {
        public ResizeForm()
        {
            InitializeComponent();
        }

        ImageForm imf;

        public ResizeForm(ImageForm imf)
        {
            this.imf = imf;
            InitializeComponent();
            Bitmap myBitmap = imf.GetBitmap();
            lblImageData.Text = ("The image is " + myBitmap.Width.ToString() + " wide and " + myBitmap.Height.ToString() + " tall.");
        }

        //Button for resizing the image via Percent.
        private void btnResize_Click_1(object sender, EventArgs e)
        {
            //Get the percent that the user wants to change the current image. Must divide by 100.
            float percentChange = float.Parse(txtInputPercent.Text) / 100;

            if (percentChange > 0)
            {
                int choice = 0;
                choice = int.Parse(cbMethod.SelectedIndex.ToString());

                //Get the current image from the picture box
                Bitmap currentImage = imf.GetBitmap();

                //Add the current image to the undo stack
                imf.undo();

                //Call the resizer function, if it worked.
                Bitmap resizedImage = Resizer.PercentResize(currentImage, percentChange, choice);

                //Update the label
                lblImageData.Text = ("The image is " + resizedImage.Width.ToString() + " wide and " + resizedImage.Height.ToString() + " tall.");

                //Set the resized image
                imf.SetBitmap(resizedImage);
                imf.imageFormBindingSource.ResetBindings(false);
            }
            else
            {
                MessageBox.Show("Please enter a number greater than 0.");
            }
        }

        //Button for adding pixels to an image.
        private void btnRelative_Click(object sender, EventArgs e)
        {
            int width = int.Parse(txtAddPixelsToWidth.Text);        //Get width and height
            int height = int.Parse(txtAddPixelsToHeight.Text);
            Bitmap currentImage = imf.GetBitmap();                  //Get the current image
            imf.undo();                                             //Add the to the undo stack
            
            //Call to Relative function
            Bitmap resizedByRelative = Resizer.Relative(currentImage, width, height);   
            lblImageData.Text = ("The image is " + resizedByRelative.Width.ToString() + " wide and " + resizedByRelative.Height.ToString() + " tall.");
            imf.SetBitmap(resizedByRelative);
        }

        //Button for setting the image pixels.
        private void btnSetImagePixels_Click(object sender, EventArgs e)
        {
            int width = int.Parse(txtSetPixelWidth.Text);           //Get width and height
            int height = int.Parse(txtSetPixelHeight.Text);         
            Bitmap currentImage = imf.GetBitmap();                  //Get the current image
            imf.undo();                                             //Add the to the undo stack
            
            //Call to Set function
            Bitmap settingImage = Resizer.Set(currentImage, width, height);
            lblImageData.Text = ("The image is " + settingImage.Width.ToString() + " wide and " + settingImage.Height.ToString() + " tall.");
            imf.SetBitmap(settingImage);
        }

       
    }
}
